package com.stockz.equity.scanner.screen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.indicator.movingAverage.simple.SimpleMovingAverageIndicator;
import com.stockz.equity.indicator.stochastics.StochasticsIndicator;
import com.stockz.equity.model.Bar;
import com.stockz.equity.model.Bar.BarValueType;
import com.stockz.equity.scanner.screen.configuration.SellingClimaxScreenConfiguration;

@Component
public class SellingClimaxScreen implements Screen<SellingClimaxScreenConfiguration> {

	@Autowired
	private SimpleMovingAverageIndicator simpleMovingAverageIndicator;
	@Autowired
	private StochasticsIndicator stochasticsIndicator;
	
	private final SellingClimaxScreenConfiguration configuration = new SellingClimaxScreenConfiguration();
	
	private int span = 20;
	private double volumeMultiple = 3;
	private double averageTradeSizeMultiple = 2;
	private double averageTradePriceMultiple = 1;
	private double stochasticsUpperLimit = 30;
	
	@Override
	public int getMandatorySpan() {
		return span;
	}
	
	@Override
	public SellingClimaxScreenConfiguration getConfiguration() {
		return configuration;
	}
	
	@Override
	public boolean isMatch(List<Bar> barList) {
		
		if(barList == null || barList.size() < getSpan()){
			return false;
		}
		
		stochasticsIndicator.setBarValueType(BarValueType.AVGPRICE);
		stochasticsIndicator.getIndicatorConfiguration().setSpan(getSpan());
		double stochastics = stochasticsIndicator.getValue(barList);

		simpleMovingAverageIndicator.getIndicatorConfiguration().setSpan(getSpan());
		simpleMovingAverageIndicator.setBarValueType(BarValueType.VOLUME);
		double vsma = simpleMovingAverageIndicator.getValue(barList);
		
		simpleMovingAverageIndicator.setBarValueType(BarValueType.AVGTRADESIZE);
		double atssma = simpleMovingAverageIndicator.getValue(barList);
		
		simpleMovingAverageIndicator.setBarValueType(BarValueType.AVGTRADEPRICE);
		double atpsma = simpleMovingAverageIndicator.getValue(barList);
		
		Bar bar = barList.get(0);
		
		if(stochastics < stochasticsUpperLimit
				&& bar.getVolume() > vsma*volumeMultiple
				&& bar.getAverageTradeSize() > atssma*averageTradeSizeMultiple
				&& bar.getAverageTradePrice() > atpsma*averageTradePriceMultiple){
		return true;	
		}
		
		return false;
	}

	public int getSpan(){
		return span;
	}
	
}
